package cn.test;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class Utils {

    /*
     * 解析听写监听器得到的JSON字符串
     */
    public static ArrayList<String> parseRecognizerResult(JSONObject jsonObject){
        ArrayList<String> list = new ArrayList<>();
        try {
            JSONArray arr = jsonObject.getJSONArray("ws");
            for (int i = 0; i < arr.length(); i++) {
                JSONObject obj = arr.getJSONObject(i);
                JSONArray childArr = obj.getJSONArray("cw");
                for (int j = 0; j < childArr.length(); j++) {
                    JSONObject childObj = childArr.getJSONObject(j);
                    list.add(childObj.getString("w"));
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static void writeRecognizerResult2File(ArrayList<String> results, File file) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
        for (int i=0;i<results.size();i++) {
            String result = results.get(i);
            writer.write(result);
        }
        writer.close();
    }

    public static void clearFile(File file) throws IOException {
        if(!file.exists() || !file.isFile()){
            file.createNewFile();
        }
        FileWriter fw = new FileWriter(file);
        fw.write("");
        fw.close();
    }

    public static ArrayList<byte[]> splitBuffer(FileInputStream in, int desLength) throws IOException {

        byte[] buffer = new byte[desLength];
        ArrayList<byte[]> buffers = new ArrayList<byte[]>();
        while((in.read(buffer))!=-1){
            buffers.add(buffer.clone());
        }
        return buffers;
    }

    private static String readFile(File file) throws Exception {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        StringBuffer sb = new StringBuffer();
        String line;
        while((line = reader.readLine())!=null){
            sb.append(line);
        }
        return sb.toString();
    }

    public static String readSrcStr() throws Exception {
        File file = new File(Config.TXTFILE);
        if(!file.exists() || !file.isFile()){
            throw new Exception("源字符文件不存在");
        }
        return readFile(file);
    }


    public static String[] readTarStr() throws Exception{
        File file = new File("src/main/resources/");
        File[] files = file.listFiles();
        StringBuffer sb = new StringBuffer();
        for (File f:files) {
            sb.append(readFile(f));
        }
        //讲文本切分成句子,以句号,分号,感叹号,问号为标志切分
        //String[] str = {"。","；","？","！",".",";","?","!"};
        String re = "。|\\.|；|;|？|\\?|！|!";
        return sb.toString().split(re);
    }


    public static JSONObject[] similar(String srcStr, String[] tarStrs, AIPClient client){
        //String
        JSONObject[] arr = new JSONObject[tarStrs.length];
        for (int i=0;i<tarStrs.length;i++) {
            String tarStr = tarStrs[i];
            arr[i] = client.simnet(tarStr,srcStr);
        }
        Arrays.sort(arr, new Comparator<JSONObject>() {
            @Override
            public int compare(JSONObject o1, JSONObject o2) {
                try {
                    if(o1.getDouble("score")>o2.getDouble("score")){
                        return -1;
                    }else if(o1.getDouble("score")<o2.getDouble("score")){
                        return 1;
                    }else{
                        return 0;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
        return Arrays.copyOfRange(arr, 0,3);
    }

    public static void main(String[] args) throws Exception {
        for (String str:readTarStr()) {
            System.out.println(str);
        }
    }

}
